package com.cheer.ba.view.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.cheer.ba.R;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.ToastUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.ShareBoardConfig;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;

import java.lang.ref.WeakReference;

/**
 * 描述 ：分享界面
 * 作者 ：hq
 * 时间 ：2017/5/27 13:22
 */
public class ShareAty extends Activity {

    private ShareAction mShareAction;

    private UMShareListener mShareListener;

    private String mTitle;

    private String mContent;

    private String mUrl;

    private String mPic;


    /**
     * 启动h5界面，带标题
     */
    public static void startShareAty(Activity activity, String title, String content, String url, String pic) {
        Intent intent = new Intent(activity, ShareAty.class);
        intent.putExtra(IntentKey.General.KEY_TITLE, title);
        intent.putExtra(IntentKey.General.KEY_CONTENT, content);
        intent.putExtra(IntentKey.General.KEY_URL, url);
        intent.putExtra(IntentKey.General.KEY_PAGE, pic);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View mView = getLayoutInflater().inflate(R.layout.aty_share, null);
        setContentView(mView);
        mView.findViewById(R.id.id_click).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showShare();
            }
        });
        initView();
    }

    public void initView() {
//        setTitleText("");
        mTitle = getIntent().getStringExtra(IntentKey.General.KEY_TITLE);
        mContent = getIntent().getStringExtra(IntentKey.General.KEY_CONTENT);
        mUrl = getIntent().getStringExtra(IntentKey.General.KEY_URL);
        mPic = getIntent().getStringExtra(IntentKey.General.KEY_PAGE);

        mShareListener = new CustomShareListener(this);
            /*增加自定义按钮的分享面板*/
        mShareAction = new ShareAction(this).setDisplayList(
                SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
                SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE)
                .setShareboardclickCallback(new ShareBoardlistener() {
                    @Override
                    public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
//                            UMWeb web = new UMWeb(Constants.Sina.REDIRECT_URL);
                        UMWeb web = new UMWeb(mUrl);
                        web.setTitle(mTitle);
                        web.setDescription(mContent);
                        if (StringUtils.isEmpty(mPic)) {
                            web.setThumb(new UMImage(ShareAty.this, R.mipmap.logo));
                        } else {
                            web.setThumb(new UMImage(ShareAty.this, mUrl));
                        }
                        new ShareAction(ShareAty.this).withMedia(web)
                                .setPlatform(share_media)
                                .setCallback(mShareListener)
                                .share();
                    }
                });
        showShare();
    }


    /**
     * 检测存储权限
     */
    private void checkPermissSdcard() {
        String[] perms = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
//        if (!mayRequestPermission(perms)) {
//            return;
//        }
        showShare();
    }

    private void showShare() {
        ToastUtil.showToast("展示出来");
        ShareBoardConfig config = new ShareBoardConfig();
        config.setMenuItemBackgroundShape(ShareBoardConfig.BG_SHAPE_NONE);
        mShareAction.open(config);
    }

//    @Override
//    public void requestPermissionResult(boolean allowPermission) {
//        if (allowPermission) {
//            showShare();
//        }
//    }

    private static class CustomShareListener implements UMShareListener {

        private WeakReference<ShareAty> mActivity;

        private CustomShareListener(ShareAty activity) {
            mActivity = new WeakReference(activity);
        }

        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            if (platform.name().equals("WEIXIN_FAVORITE")) {
                Toast.makeText(mActivity.get(), platform + " 收藏成功啦", Toast.LENGTH_SHORT).show();
            } else {
                if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                        && platform != SHARE_MEDIA.EMAIL) {
                    Toast.makeText(mActivity.get(), platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
                }
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                    && platform != SHARE_MEDIA.EMAIL
                    && platform != SHARE_MEDIA.FLICKR
                    && platform != SHARE_MEDIA.FOURSQUARE
                    && platform != SHARE_MEDIA.TUMBLR
                    && platform != SHARE_MEDIA.POCKET
                    && platform != SHARE_MEDIA.PINTEREST

                    && platform != SHARE_MEDIA.INSTAGRAM
                    && platform != SHARE_MEDIA.GOOGLEPLUS
                    && platform != SHARE_MEDIA.YNOTE
                    && platform != SHARE_MEDIA.EVERNOTE) {
                Toast.makeText(mActivity.get(), platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
                if (t != null) {
                    Log.d("throw", "throw:" + t.getMessage());
                }
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(mActivity.get(), platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** attention to this below ,must add this**/
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
